﻿using AutoMapper;
using B.S.Filtration.Domain;
using B.S.Filtration.ErrorCode;
using B.S.Filtration.Read.Api.Application.Command.FiltrateCommand;
using MediatR;
using SqlSugar;

namespace B.S.Filtration.Read.Api.Application.CommandHander.FiltrateCommandHander
{
    public class ShowFiltrateCommandHander : IRequestHandler<ShowFiltrateCommand, APIResult<List<Filtrate>>>
    {
        private readonly ISqlSugarClient db;
        private readonly IMapper mapper;

        public ShowFiltrateCommandHander(ISqlSugarClient db, IMapper mapper)
        {
            this.db = db;
            this.mapper = mapper;
        }

        public async Task<APIResult<List<Filtrate>>> Handle(ShowFiltrateCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<Filtrate>> result = new APIResult<List<Filtrate>>();

            var list = await db.Queryable<Filtrate>()
                                .Where(x => x.Del == false && x.FiltrateId == request.FiltrateId)
                                .ToListAsync();

            result.data = list;

            result.code = APIResultCode.success;
            result.msg = "查询成功";

            return await Task.FromResult(result);
        }
    }
}
